package kmdm.security

import java.util.Date
import javax.inject.Inject
import org.bson.codecs.configuration.CodecRegistries.fromProviders
import org.bson.codecs.configuration.CodecRegistries.fromRegistries
import org.bson.codecs.configuration.CodecRegistry
import org.bson.types.ObjectId
import org.mongodb.scala.model.Filters.equal
import play.api.Logger
import play.api.inject.Injector

/**
  * Created by deanzhang on 17/8/11.
  */
class UserDao @Inject()(implicit val injector: Injector) extends AbstractDaoImpl[User] {
  override protected def collectionName: String = "kmdm_users"
  import org.mongodb.scala.bson.codecs.Macros._
  private implicit val codecRegistry: CodecRegistry = fromRegistries(defaultRegistry, fromProviders(classOf[User]))

  /*
      public KmdmUsersBean getByUserID(String userid) {
        if (StringUtils.isEmpty(userid))
            return null;
        Query<KmdmUsersBean> query = this.getDatastore().createQuery(KmdmUsersBean.class);
        query.filter("userid", userid);
        return query.get();
    }
   */
  def getByUserID(userId: String): Option[User] = {
    log.info(s"get user by id [$userId]")
    if (userId.isEmpty) None
    else {
      Some(getCollection.find(equal("userid", userId)).head())
    }
  }

  def getAll: Seq[User] = super.getAll

}

case class User(
    _id: ObjectId,
    username: String,
    userid: String,
    password: String,
    nick: String,
    createTime: Date,
    updateTime: Date = new Date,
    userType: Int = 0
)
object UserDao{
  def main (args: Array[String] ): Unit = {
//    getAll
  }

}
